package com.warmheart.controller.sys;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.warmheart.base.annotation.Auth;
import com.warmheart.base.annotation.Duplicate;
import com.warmheart.base.config.AliSmsService;
import com.warmheart.base.config.RedisService;
import com.warmheart.base.controller.BaseController;
import com.warmheart.core.constant.RedisConstant;
import com.warmheart.core.enums.PlatEnum.MobileMsgTypeEnum;
import com.warmheart.core.enums.ResultCode;
import com.warmheart.core.result.CommonResult;
import com.warmheart.core.util.ParamMapUtil;
import com.warmheart.model.CheckResultModel;

/**
 * 
 * @ClassName: AliSmsController
 * @Description: 发送短信验证码控制器
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@RestController
@RequestMapping("/sms")
public class AliSmsController extends BaseController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private AliSmsService aliSmsService;

    /**
     * 
     * @Title: sendRegisterSmsCode
     * @Description: 发送注册验证码
     * @author ZhangJianyang
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/sendRegisterSmsCode", method = RequestMethod.POST)
    public CommonResult<Object> sendRegisterSmsCode(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String userPhone = ParamMapUtil.getString(jsonMap, "userPhone");
        if (StringUtils.isBlank(userPhone)) {
            // 手机号码不可为空
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "手机号码不可为空");
        }
        // 验证手机号码
        if (userPhone.length() != 11) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "手机号码格式不正确");
        }
        Pattern userPhonePattern = Pattern.compile("^([1]([3-9])[0-9]{9})$");
        boolean userPhonePatternCheckResult = userPhonePattern.matcher(userPhone).matches();
        if (!userPhonePatternCheckResult) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "手机号码格式不正确");
        }
        // 限制校验
        CheckResultModel verifiedResult = aliSmsService.checkPhone(userPhone, MobileMsgTypeEnum.REGISTER_USER_MSG);
        if (!verifiedResult.getCheckFlag()) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, verifiedResult.getCheckMsg());
        }
        String smsCode = RandomUtil.randomNumbers(6);
        // String smsCode = RandomUtils.getRandomString(6);
        // boolean sendFlag = aliSmsService.sendLoginMsg(userPhone, smsCode);
        // if (!sendFlag) {
        // // 返回发送验证码失败
        // return CommonResult.failed("验证码发送失败");
        // }
        // 缓存验证码并设置300秒过期时间
        redisService.set(RedisConstant.REGISTER_SMS_CODE + userPhone, smsCode, 300);
        // 返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("smsCode", smsCode);
        // 返回数据信息
        return CommonResult.success("获取验证码成功", resultMap);
    }

    /**
     * 
     * @Title: sendModifyPhoneSmsCode
     * @Description: 发送修改手机号码验证码
     * @author ZhangJianyang
     * @param request
     * @return
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @Duplicate(validate = true)
    @RequestMapping(value = "/sendModifyPhoneSmsCode", method = RequestMethod.POST)
    public CommonResult<Object> sendModifyPhoneSmsCode(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String userPhone = ParamMapUtil.getString(jsonMap, "userPhone");
        if (StringUtils.isBlank(userPhone)) {
            // 手机号码不可为空
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "手机号码不可为空");
        }
        // 验证手机号码
        if (userPhone.length() != 11) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "手机号码格式不正确");
        }
        Pattern userPhonePattern = Pattern.compile("^([1]([3-9])[0-9]{9})$");
        boolean userPhonePatternCheckResult = userPhonePattern.matcher(userPhone).matches();
        if (!userPhonePatternCheckResult) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "手机号码格式不正确");
        }
        // 限制校验
        CheckResultModel verifiedResult = aliSmsService.checkPhone(userPhone, MobileMsgTypeEnum.MODIFY_MOBILE_MSG);
        if (!verifiedResult.getCheckFlag()) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, verifiedResult.getCheckMsg());
        }
        String smsCode = "123456";
        // String smsCode = RandomUtils.getRandomString(6);
        // boolean sendFlag = aliSmsService.sendLoginMsg(userPhone, smsCode);
        // if (!sendFlag) {
        // // 返回发送验证码失败
        // return CommonResult.failed("验证码发送失败");
        // }
        // 缓存验证码并设置300秒过期时间
        redisService.set(RedisConstant.MODIFY_PHONE_SMS_CODE + userPhone, smsCode, 300);
        // 返回数据信息
        return CommonResult.success("验证码发送成功");
    }
}
